package com.duanjc.mergemd.execute;

import com.duanjc.mergemd.bean.DocumentContent;
import com.duanjc.mergemd.service.MergeToMultipleFile;
import com.duanjc.mergemd.service.MergeToOneFile;
import com.duanjc.mergemd.utils.FileCheckUtils;
import com.duanjc.mergemd.utils.FileIOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * [一句话描述该类的功能]
 *
 * @author : [duanjiangcheng]
 * @version : [v1.0]
 * @createTime : [2022/11/24 17:40]
 */
@Component
@Slf4j
public class ExecuteApplication implements ApplicationRunner {

    @Autowired
    private MergeToOneFile mergeToOneFile;

    @Autowired
    private MergeToMultipleFile mergeToMultipleFile;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        //long start = System.currentTimeMillis();
        ////读取合并配置文件
        //DocumentContent fileContent = null;
        //try {
        //    fileContent = FileIOUtils.read("merge.properties");
        //    log.info("merge.properties--->:{}", fileContent.toString());
        //} catch (FileNotFoundException e) {
        //    log.error("读取[merge.properties]文件时出现了异常", e);
        //}
        //execute(fileContent);
        //long end = System.currentTimeMillis();
        //log.info("总共耗时：" + (end - start) / 1000.0 + "秒！");
        //System.exit(0);
    }

    private void execute(DocumentContent fileContent) {
        int choose = fileContent.getChoose();
        try {
            switch (choose) {
                case 0:
                    FileCheckUtils.checkDestPathFormat(fileContent);
                    mergeToOneFile.setDocumentContent(fileContent);
                    mergeToOneFile.operationMerge();
                    break;
                case 1:
                    mergeToMultipleFile.setDocumentContent(fileContent);
                    mergeToMultipleFile.operationMerge();
                    break;
                default:
                    break;
            }
        } catch (IOException e) {
            log.error("合并文件失败！",e);
        }
    }
}
